/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zzz.view.managerControl;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.zzz.controller.ClassController;
import org.zzz.controller.StudentController;
import org.zzz.controller.StudentCourseController;
import org.zzz.controller.UserController;
import org.zzz.entity.StudentCourse;
import org.zzz.entity.StudentVo;
import org.zzz.entity.vo.StudentShowVo;
import org.zzz.utils.Result;
import org.zzz.utils.ResultEnum;
import org.zzz.view.util.UiUtil;

import java.util.ArrayList;

/**
 *
 * @author AFun
 */
public class managerStuSelect extends javax.swing.JFrame {

    /**
     * Creates new form managerStuSelect
     */
    public managerStuSelect() {
        initComponents();
        init();         //初始化
    }
    private void init(){
        this.setTitle("学生信息操作");
        UiUtil.setFrameCenter(this);
        this.setResizable(false);
        this.is_exist=false;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        chaxun = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        jScrollPane1.setViewportView(jEditorPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("学号/姓名");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "学号", "姓名" }));

        jLabel2.setText("根据");

        jButton2.setText("取消");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        chaxun.setText("查询");
        chaxun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chaxunActionPerformed(evt);
            }
        });

        jTable1.setBackground(new java.awt.Color(214, 217, 223));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                        {null, null, null, null, null}
                },
                new String [] {
                        "班级", "学号", "姓名", "性别", "课程"
                }
        ) {
            boolean[] canEdit = new boolean [] {
                    false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable1);

        jButton4.setText("修改");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("删除");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(57, 57, 57)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jLabel2)
                                                                        .addComponent(jLabel1))
                                                                .addGap(48, 48, 48)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                        .addGroup(layout.createSequentialGroup()
                                                                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                .addComponent(chaxun))
                                                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(81, 81, 81)
                                                .addComponent(jButton4)
                                                .addGap(67, 67, 67)
                                                .addComponent(jButton5)
                                                .addGap(61, 61, 61)
                                                .addComponent(jButton2)))
                                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2)
                                        .addComponent(chaxun))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton2)
                                        .addComponent(jButton4)
                                        .addComponent(jButton5))
                                .addGap(36, 36, 36))
        );

        pack();
    }// </editor-fold>

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        manager_stuMessage msm=new manager_stuMessage();
        this.dispose();
        msm.setVisible(true);
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if(this.is_exist){
            managerStuAlter msa=new managerStuAlter(this.m_Class,this.m_Nums,
                    this.m_Name,this.m_Sex,this.m_Course);
            this.dispose();
            msa.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(null, "此人不存在，不能进行修改操作！");
        }

    }

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if(this.is_exist){
            //对数据库进行信息删除
            if (JOptionPane.showConfirmDialog(null, "是否删除？", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                //是：将文本框清空
                UserController userController = new UserController();
                StudentCourseController studentCourseController = new StudentCourseController();
                //删除学生和用户
                userController.deleteUser(this.m_Nums,1);
                //删除学生课程关联
                Result<StudentCourse> getScResult = studentCourseController.getStudentCourseBySid(this.m_Nums);
                if (getScResult.getStatus() == ResultEnum.SUCCESS.getStatus()){
                    studentCourseController.deleteSCById(getScResult.getData().getId());
                }
                System.out.println("删除");
                JOptionPane.showMessageDialog(null, "删除成功！");
                //表格清空
                DefaultTableModel tableModel = (DefaultTableModel)this.jTable1.getModel();
                if(tableModel.getColumnCount()!=0){
                    tableModel.removeRow(0);
                }

                this.jTextField1.setText("");
            }
        }else{
            JOptionPane.showMessageDialog(null, "此人不存在，不能进行删除操作！");
        }
    }

    private void chaxunActionPerformed(java.awt.event.ActionEvent evt) {
//        System.out.println("aaaa");
        //将获取到的数据填进去
        String n_class="";
        String n_Nums="";
        String n_Name="";
        String n_Sex="";
        String n_Course="";
        //查找成功
        this.is_exist=true;

        //获取查找是否成功状态
        // TODO add your handling code here:
        // TODO add your handling code here:
        //获取表框内数据
        String messageXX=this.jTextField1.getText();
        String genju=this.jComboBox1.getSelectedItem().toString();
        //从数据库获取数据
        UserController userController = new UserController();
        ClassController classController = new ClassController();
        StudentController studentController = new StudentController();
        DefaultTableModel tableModel = (DefaultTableModel)this.jTable1.getModel();
        if(tableModel.getRowCount()!=0){
            tableModel.removeRow(0);
        }

        if("学号".equals(genju)){

//            Result<StudentVo> studentResult = userController.getStudentById(messageXX);
            Result<StudentShowVo> studentResult = studentController.getStudentById(messageXX);
//            DefaultTableModel tableModel = (DefaultTableModel)this.jTable1.getModel();
//            tableModel.removeRow(0);

            //存在该学号的信息
            if(studentResult.getStatus()== ResultEnum.SUCCESS.getStatus()){
                //查询成功
                StudentShowVo studentShowVo = studentResult.getData();
                this.m_Class=studentShowVo.getClassName();
                this.m_Nums=studentShowVo.getId();
                this.m_Name=studentShowVo.getName();
                this.m_Sex=studentShowVo.getGender();
                this.m_Course=studentShowVo.getCourseName();

                tableModel.addRow(new Object[]{studentShowVo.getClassName(),studentShowVo.getId(),
                studentShowVo.getName(),studentShowVo.getGender(),studentShowVo.getCourseName()});

            }else{
                //查不到
                JOptionPane.showMessageDialog(this, "查询不到此人信息");
            }


        }else{
            //姓名
            Result<StudentShowVo> nameResult = studentController.getStudentByName(messageXX);
            if (nameResult.getStatus() == ResultEnum.SUCCESS.getStatus()){
                //查到
                StudentShowVo studentShowVo = nameResult.getData();
                this.m_Class=studentShowVo.getClassName();
                this.m_Nums=studentShowVo.getId();
                this.m_Name=studentShowVo.getName();
                this.m_Sex=studentShowVo.getGender();
                this.m_Course=studentShowVo.getCourseName();
//                DefaultTableModel tableModel = (DefaultTableModel)this.jTable1.getModel();
                //删除所有
                tableModel.addRow(new Object[]{studentShowVo.getClassName(),studentShowVo.getId(),
                        studentShowVo.getName(),studentShowVo.getGender(),studentShowVo.getCourseName()});

            }else{
                //查不到
                JOptionPane.showMessageDialog(this, "查询不到此人信息");
            }
        }


        if(false){
            JOptionPane.showMessageDialog(this, "查询不到此人信息");
        }else{
            //将获取到的信息逐行显示
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(managerStuSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(managerStuSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(managerStuSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(managerStuSelect.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new managerStuSelect().setVisible(true);
            }
        });
    }

    private String m_Class;
    private String m_Nums;
    private String m_Name;
    private String m_Sex;
    private String m_Course;
    private boolean is_exist;

    // Variables declaration - do not modify
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton chaxun;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}